<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">

<html><head><title>Simple PHP Proxy: Get external HTML, JSON and more!</title><link rel="stylesheet" type="text/css" href="../styles/main.css"><script language=JavaScript src="../javascript/main.js"></script><script language=JavaScript src="../javascript/searchdata.js"></script></head><body class="ContentPage" onLoad="NDOnLoad()"><script language=JavaScript><!--
if (browserType) {document.write("<div class=" + browserType + ">");if (browserVer) {document.write("<div class=" + browserVer + ">"); }}// --></script>

<!--  Generated by Natural Docs, version 1.4 -->
<!--  http://www.naturaldocs.org  -->

<!-- saved from url=(0026)http://www.naturaldocs.org -->




<div id=Content><div class="CFile"><div class=CTopic id=MainTopic><h1 class=CTitle><a name="Simple_PHP_Proxy:Get_external_HTML,JSON_and_more!"></a>Simple PHP Proxy: Get external HTML, JSON and more!</h1><div class=CBody><p><b>Version: 1.6, Last updated: 1/24/2009</b></p><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>Project Home</td><td class=CDLDescription><a href="http://benalman.com/projects/php-simple-proxy/" class=LURL target=_top>http://benalman.com/projects/php-simple-proxy/</a></td></tr><tr><td class=CDLEntry>GitHub</td><td class=CDLDescription><a href="http://github.com/cowboy/php-simple-proxy/" class=LURL target=_top>http://github.com/cowboy/php-simple-proxy/</a></td></tr><tr><td class=CDLEntry>Source</td><td class=CDLDescription><a href="http://github.com/cowboy/php-simple-proxy/raw/master/ba-simple-proxy.php" class=LURL target=_top>http://github.com<wbr>/cowboy<wbr>/php-simple-proxy<wbr>/raw<wbr>/master<wbr>/ba-simple-proxy.php</a></td></tr></table><!--START_ND_SUMMARY--><div class=Summary><div class=STitle>Summary</div><div class=SBorder><table border=0 cellspacing=0 cellpadding=0 class=STable><tr class="SMain"><td class=SEntry><a href="#Simple_PHP_Proxy:Get_external_HTML,JSON_and_more!" >Simple PHP Proxy: Get external HTML, JSON and more!</a></td><td class=SDescription><b>Version: 1.6, Last updated: 1/24/2009</b></td></tr><tr class="SGeneric SMarked"><td class=SEntry><a href="#License" >License</a></td><td class=SDescription>Copyright &copy; 2010 &ldquo;Cowboy&rdquo; Ben Alman, Dual licensed under the MIT and GPL licenses. </td></tr><tr class="SGeneric"><td class=SEntry><a href="#Examples" >Examples</a></td><td class=SDescription>This working example, complete with fully commented code, illustrates one way in which this PHP script can be used.</td></tr><tr class="SGeneric SMarked"><td class=SEntry><a href="#Release_History" >Release History</a></td><td class=SDescription></td></tr><tr class="SGeneric"><td class=SEntry><a href="#GET_Parameters" >GET Parameters</a></td><td class=SDescription>Certain GET (query string) parameters may be passed into ba-simple-proxy.php to control its behavior, this is a list of these parameters.</td></tr><tr class="SGeneric SMarked"><td class=SEntry><a href="#POST_Parameters" >POST Parameters</a></td><td class=SDescription>All POST parameters are automatically passed through to the remote URL request.</td></tr><tr class="SGeneric"><td class=SEntry><a href="#JSON_requests" >JSON requests</a></td><td class=SDescription>This request will return the contents of the specified url in JSON format.</td></tr><tr class="SGeneric SMarked"><td class=SEntry><a href="#JSONP_requests" >JSONP requests</a></td><td class=SDescription>This request will return the contents of the specified url in JSONP format (but only if $enable_jsonp is enabled in the PHP script).</td></tr><tr class="SGeneric"><td class=SEntry><a href="#Native_requests" >Native requests</a></td><td class=SDescription>This request will return the contents of the specified url in the format it was received in, including the same content-type and other headers (but only if $enable_native is enabled in the PHP script).</td></tr><tr class="SGeneric SMarked"><td class=SEntry><a href="#Notes" >Notes</a></td><td class=SDescription></td></tr><tr class="SGeneric"><td class=SEntry><a href="#Configuration_Options" >Configuration Options</a></td><td class=SDescription>These variables can be manually edited in the PHP file if necessary.</td></tr></table></div></div><!--END_ND_SUMMARY--></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="License"></a>License</h3><div class=CBody><p>Copyright &copy; 2010 &ldquo;Cowboy&rdquo; Ben Alman, Dual licensed under the MIT and GPL licenses.&nbsp; <a href="http://benalman.com/about/license/" class=LURL target=_top>http://benalman.com/about/license/</a></p></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="Examples"></a>Examples</h3><div class=CBody><p>This working example, complete with fully commented code, illustrates one way in which this PHP script can be used.</p><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>Simple</td><td class=CDLDescription><a href="http://benalman.com/code/projects/php-simple-proxy/examples/simple/" class=LURL target=_top>http://benalman.com<wbr>/code<wbr>/projects<wbr>/php-simple-proxy<wbr>/examples<wbr>/simple<wbr>/</a></td></tr></table></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="Release_History"></a>Release History</h3><div class=CBody><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>1.6</td><td class=CDLDescription>(1/24/2009) Defaults to JSON mode, which can now be changed to native mode by specifying ?mode=native.&nbsp; Native and JSONP modes are disabled by default, because of possible XSS issues, but are configurable in the PHP script along with a url validation regex.</td></tr><tr><td class=CDLEntry>1.5</td><td class=CDLDescription>(12/27/2009) Initial release</td></tr></table></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="GET_Parameters"></a>GET Parameters</h3><div class=CBody><p>Certain GET (query string) parameters may be passed into ba-simple-proxy.php to control its behavior, this is a list of these parameters.</p><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>url</td><td class=CDLDescription>The remote URL resource to fetch.&nbsp; Any GET parameters to be passed through to the remote URL resource must be urlencoded in this parameter.</td></tr><tr><td class=CDLEntry>mode</td><td class=CDLDescription>If mode=native, the response will be sent using the same content type and headers that the remote URL resource returned.&nbsp; If omitted, the response will be JSON (or JSONP).&nbsp; <a href="#Native_requests" class=LGeneric id=link1 onMouseOver="ShowTip(event, 'tt1', 'link1')" onMouseOut="HideTip('tt1')">Native requests</a> and <a href="#JSONP_requests" class=LGeneric id=link2 onMouseOver="ShowTip(event, 'tt2', 'link2')" onMouseOut="HideTip('tt2')">JSONP requests</a> are disabled by default, see <a href="#Configuration_Options" class=LGeneric id=link3 onMouseOver="ShowTip(event, 'tt3', 'link3')" onMouseOut="HideTip('tt3')">Configuration Options</a> for more information.</td></tr><tr><td class=CDLEntry>callback</td><td class=CDLDescription>If specified, the response JSON will be wrapped in this named function call.&nbsp; This parameter and <a href="#JSONP_requests" class=LGeneric id=link4 onMouseOver="ShowTip(event, 'tt2', 'link4')" onMouseOut="HideTip('tt2')">JSONP requests</a> are disabled by default, see <a href="#Configuration_Options" class=LGeneric id=link5 onMouseOver="ShowTip(event, 'tt3', 'link5')" onMouseOut="HideTip('tt3')">Configuration Options</a> for more information.</td></tr><tr><td class=CDLEntry>user_agent</td><td class=CDLDescription>This value will be sent to the remote URL request as the `User-Agent:` HTTP request header.&nbsp; If omitted, the browser user agent will be passed through.</td></tr><tr><td class=CDLEntry>send_cookies</td><td class=CDLDescription>If send_cookies=1, all cookies will be forwarded through to the remote URL request.</td></tr><tr><td class=CDLEntry>send_session</td><td class=CDLDescription>If send_session=1 and send_cookies=1, the SID cookie will be forwarded through to the remote URL request.</td></tr><tr><td class=CDLEntry>full_headers</td><td class=CDLDescription>If a JSON request and full_headers=1, the JSON response will contain detailed header information.</td></tr><tr><td class=CDLEntry>full_status</td><td class=CDLDescription>If a JSON request and full_status=1, the JSON response will contain detailed cURL status information, otherwise it will just contain the `http_code` property.</td></tr></table></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="POST_Parameters"></a>POST Parameters</h3><div class=CBody><p>All POST parameters are automatically passed through to the remote URL request.</p></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="JSON_requests"></a>JSON requests</h3><div class=CBody><p>This request will return the contents of the specified url in JSON format.</p><h4 class=CHeading>Request</h4><blockquote><pre>ba-simple-proxy.php?url=http://example.com/</pre></blockquote><h4 class=CHeading>Response</h4><blockquote><pre>{ &quot;contents&quot;: &quot;&lt;html&gt;...&lt;/html&gt;&quot;, &quot;headers&quot;: {...}, &quot;status&quot;: {...} }</pre></blockquote><h4 class=CHeading>JSON object properties</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>contents</td><td class=CDLDescription>(String) The contents of the remote URL resource.</td></tr><tr><td class=CDLEntry>headers</td><td class=CDLDescription>(Object) A hash of HTTP headers returned by the remote URL resource.</td></tr><tr><td class=CDLEntry>status</td><td class=CDLDescription>(Object) A hash of status codes returned by cURL.</td></tr></table></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="JSONP_requests"></a>JSONP requests</h3><div class=CBody><p>This request will return the contents of the specified url in JSONP format (but only if $enable_jsonp is enabled in the PHP script).</p><h4 class=CHeading>Request</h4><blockquote><pre>ba-simple-proxy.php?url=http://example.com/&amp;callback=foo</pre></blockquote><h4 class=CHeading>Response</h4><blockquote><pre>foo({ &quot;contents&quot;: &quot;&lt;html&gt;...&lt;/html&gt;&quot;, &quot;headers&quot;: {...}, &quot;status&quot;: {...} })</pre></blockquote><h4 class=CHeading>JSON object properties</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>contents</td><td class=CDLDescription>(String) The contents of the remote URL resource.</td></tr><tr><td class=CDLEntry>headers</td><td class=CDLDescription>(Object) A hash of HTTP headers returned by the remote URL resource.</td></tr><tr><td class=CDLEntry>status</td><td class=CDLDescription>(Object) A hash of status codes returned by cURL.</td></tr></table></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="Native_requests"></a>Native requests</h3><div class=CBody><p>This request will return the contents of the specified url in the format it was received in, including the same content-type and other headers (but only if $enable_native is enabled in the PHP script).</p><h4 class=CHeading>Request</h4><blockquote><pre>ba-simple-proxy.php?url=http://example.com/&amp;mode=native</pre></blockquote><h4 class=CHeading>Response</h4><blockquote><pre>&lt;html&gt;...&lt;/html&gt;</pre></blockquote></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="Notes"></a>Notes</h3><div class=CBody><ul><li>Assumes magic_quotes_gpc = Off in php.ini</li></ul></div></div></div>

<div class="CGeneric"><div class=CTopic><h3 class=CTitle><a name="Configuration_Options"></a>Configuration Options</h3><div class=CBody><p>These variables can be manually edited in the PHP file if necessary.</p><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$enable_jsonp</td><td class=CDLDescription>Only enable <a href="#JSONP_requests" class=LGeneric id=link6 onMouseOver="ShowTip(event, 'tt2', 'link6')" onMouseOut="HideTip('tt2')">JSONP requests</a> if you really need to.&nbsp; If you install this script on the same server as the page you&rsquo;re calling it from, plain JSON will work.&nbsp; Defaults to false.</td></tr><tr><td class=CDLEntry>$enable_native</td><td class=CDLDescription>You can enable <a href="#Native_requests" class=LGeneric id=link7 onMouseOver="ShowTip(event, 'tt1', 'link7')" onMouseOut="HideTip('tt1')">Native requests</a>, but you should only do this if you also whitelist specific URLs using $valid_url_regex, to avoid possible XSS vulnerabilities.&nbsp; Defaults to false.</td></tr><tr><td class=CDLEntry>$valid_url_regex</td><td class=CDLDescription>This regex is matched against the url parameter to ensure that it is valid.&nbsp; This setting only needs to be used if either $enable_jsonp or $enable_native are enabled.&nbsp; Defaults to &lsquo;/.*/&rsquo; which validates all URLs.</td></tr></table></div></div></div>

</div><!--Content-->


<div id=Footer><a href="http://www.naturaldocs.org">Generated by Natural Docs</a></div><!--Footer-->


<div id=Menu><div class=MEntry><div class=MFile id=MSelected>Simple PHP Proxy</div></div><div class=MEntry><div class=MGroup><a href="javascript:ToggleMenu('MGroupContent1')">Index</a><div class=MGroupContent id=MGroupContent1><div class=MEntry><div class=MIndex><a href="../index/General.html">Everything</a></div></div><div class=MEntry><div class=MIndex><a href="../index/Files.html">Files</a></div></div></div></div></div><script type="text/javascript"><!--
var searchPanel = new SearchPanel("searchPanel", "HTML", "../search");
--></script><div id=MSearchPanel class=MSearchPanelInactive><input type=text id=MSearchField value=Search onFocus="searchPanel.OnSearchFieldFocus(true)" onBlur="searchPanel.OnSearchFieldFocus(false)" onKeyUp="searchPanel.OnSearchFieldChange()"><select id=MSearchType onFocus="searchPanel.OnSearchTypeFocus(true)" onBlur="searchPanel.OnSearchTypeFocus(false)" onChange="searchPanel.OnSearchTypeChange()"><option  id=MSearchEverything selected value="General">Everything</option><option value="Files">Files</option></select></div></div><!--Menu-->



<!--START_ND_TOOLTIPS-->
<div class=CToolTip id="tt1"><div class=CGeneric>This request will return the contents of the specified url in the format it was received in, including the same content-type and other headers (but only if $enable_native is enabled in the PHP script).</div></div><div class=CToolTip id="tt2"><div class=CGeneric>This request will return the contents of the specified url in JSONP format (but only if $enable_jsonp is enabled in the PHP script).</div></div><div class=CToolTip id="tt3"><div class=CGeneric>These variables can be manually edited in the PHP file if necessary.</div></div><!--END_ND_TOOLTIPS-->




<div id=MSearchResultsWindow><iframe src="" frameborder=0 name=MSearchResults id=MSearchResults></iframe><a href="javascript:searchPanel.CloseResultsWindow()" id=MSearchResultsWindowClose>Close</a></div>


<script language=JavaScript><!--
if (browserType) {if (browserVer) {document.write("</div>"); }document.write("</div>");}// --></script></body></html>